import Vue from 'vue'
import Router from 'vue-router'
import index from './views/Index'

Vue.use(Router)

const router = new Router({
  mode: "history",
  base: process.env.BASE_URL,
  routes: [
    {
      path: "/",
      name: "index",
      redirect: "/chats",
      component: index,
      // 把微信和发现等页面当子路由引入
      children: [
        {
          path: "/chats",
          name: "chats",
          component: () => import("./views/Chats.vue")
        },
        {
          path: "/contacts",
          name: "contacts",
          component: () => import("./views/Contacts.vue")
        },
        {
          path: "/discover",
          name: "discover",
          component: () => import("./views/Discover.vue")
        },
        {
          path: "/me",
          name: "me",
          component: () => import("./views/Me.vue")
        }
      ]
    },
    {
      path: "/login",
      name: "login",
      // 懒加载方式,点击的时候才会生效
      component: () => import("./views/Login.vue")
    },
    {
      path: "/register",
      name: "register",
      component: () => import("./views/Register.vue")
    },
    {
      path: "/information",
      name: "information",
      component: () => import("./views/Information.vue")
    },
    {
      path: "/chat",//这个:id为路由传参的属性名
      name: "chat",
      component: () => import("./views/ChatView.vue"),
      meta: { keepAlive: true }
    }
  ]
});

// 登录之前判断有没有先登录 
router.beforeEach((to, from, next) => {
  // 如果有token就证明登录成功 
  const isLogin = localStorage.wxToken ? true : false;
  if (to.path == '/login' || to.path == '/register') {
    // 如果当前页面为登录或者注册就让正常访问
    next()
  } else {
    // 如果在别的页面有登录就正常访问;没有登录 就进入登录页面;
    isLogin ? next() : next('/login')
  }
})
  


export default router;
